﻿using System;

using WeDonekRpc.PipeServer.Interface;

namespace WeDonekRpc.PipeServer.Server
{
    internal class ServerInfo
    {
        /// <summary>
        /// 客户端的管理类
        /// </summary>
        private readonly Manage.ClientManage _ClientInfo = null;

        private readonly string _PublicKey = null;


        private readonly string _PipeName;
        protected Manage.ClientManage ClientInfo => this._ClientInfo;
        internal string PublicKey => this._PublicKey;
        internal int ClientCount => this._ClientInfo.ClientCount;

        public string PipeName => this._PipeName;

        internal ServerInfo (string pipeName, IAllot allot, IPipeEvent socketEvent, string publicKey)
        {
            this._PipeName = pipeName;
            this._PublicKey = publicKey;
            this._ClientInfo = new Manage.ClientManage(allot, socketEvent, this);
        }
        internal ServerInfo ()
        {
            this._ClientInfo = new Manage.ClientManage();
        }


        internal bool GetClient (Guid clientId, out IClient client, out string error)
        {
            return this._ClientInfo.GetClient(clientId, out client, out error);
        }


        internal void CloseServer ()
        {
            this._ClientInfo.CloseClient();
        }

        /// <summary>
        /// 初始化服务
        /// </summary>
        /// <param name="port"></param>
        internal void InitServer ()
        {
            this._ClientInfo.Add();
        }

        internal void CheckIsCon ()
        {
            this._ClientInfo?.CheckIsCon();
        }

        internal void CloseClient (Guid clientId)
        {
            this._ClientInfo?.CloseClient(clientId);
        }

        internal void ClientCloseEvent (Guid clientId)
        {
            this._ClientInfo.ClientCloseEvent(clientId);
        }

        internal IClient FindClient (string bindParam)
        {
            return this._ClientInfo.Find(bindParam);
        }
    }
}
